<?php

/**
 * Example - Comaprison of Non-structured -> Structured -> Object Oriented approaches
 * 
 * Task: Display a name and email for every user record in the database
 */ 

// Version 1 - Non-structured approach

$sql_results = $db->query('SELECT * FROM users');

foreach( $user_data as $row ) {

	echo 'Name: ' . $row['name'];
	echo 'Email: ' . $row['email'];

}

// Version 2 - Structured approach

function get_users() {

	$user_data = $db->query('SELECT * FROM users');

	return $user_data;

}

$users = get_users();

foreach( $users as $user ) {

	echo 'Name: ' . $user['name'];
	echo 'Email: ' . $user['email'];

}

// Version 3 - Object Oriented Approach

/**
 * User manager class definition
 * 
 * Would be stored in a separte file, such as class.usermanager.php, and required as needed
 */ 
class UserManager {

	private $users;

	/**
	 * Constructor method
	 * 
	 * This method is called automatically whenever a new user manager object is created, i.e.
	 * 	$user_manager = new UserManager()
	 */ 
	public function __construct() {

		$this->load_users();

	}

	/**
	 * Load users from the database and store in instance variable $users
	 */ 
	private function load_users() {

		$this->users = $db->query('SELECT * FROM users');

	}

	/**
	 * Print a list of users
	 */ 
	public function list_users() {

		foreach( $this->users as $user ) {

			echo 'Name: ' . $user['name'];
			echo 'Email: ' . $user['email'];

		}
	}

}

// Client code for listing users
$user_manager = new UserManager();
$user_manager->list_users();
